package com.red.wine.ui;

import com.red.wine.R;
import com.red.wine.util.IntentUtil;
import com.red.wine.util.PrefsUtils;
import com.red.wine.view.MenuView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnGestureListener {

	private GestureDetector detector;

	/**
     */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		context = this;
		initMenuAndCircle();
		detector = new GestureDetector(this);
	}

	Bitmap taste_bitmap;
	MenuView menuview;
	Bitmap taste_bitmap1;
	MenuView menuview1;
	Bitmap taste_bitmap2;
	MenuView menuview2;
	Bitmap taste_bitmap3;
	MenuView menuview3;
	Bitmap taste_bitmap4;
	MenuView menuview4;
	Bitmap taste_bitmap5;
	MenuView menuview5;
	int height, width;
	private FrameLayout menu_layout;
	Context context;

	private void initMenuAndCircle() {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		height = displayMetrics.heightPixels;
		width = displayMetrics.widthPixels;

		menu_layout = (FrameLayout) findViewById(R.id.menu_img);
		taste_bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.taste_press);
		menuview = new MenuView(context, taste_bitmap, width, height, -15, 0);
		menu_layout.addView(menuview);
		taste_bitmap1 = BitmapFactory.decodeResource(getResources(),
				R.drawable.appreciate_normal);
		menuview1 = new MenuView(context, taste_bitmap1, width, height, 45, 1);
		menu_layout.addView(menuview1);
		taste_bitmap2 = BitmapFactory.decodeResource(getResources(),
				R.drawable.recommend_normal);
		menuview2 = new MenuView(context, taste_bitmap2, width, height, 105, 2);
		menu_layout.addView(menuview2);
		taste_bitmap3 = BitmapFactory.decodeResource(getResources(),
				R.drawable.purchase_normal);
		menuview3 = new MenuView(context, taste_bitmap3, width, height, 165, 3);
		menu_layout.addView(menuview3);
		taste_bitmap4 = BitmapFactory.decodeResource(getResources(),
				R.drawable.community_normal);
		menuview4 = new MenuView(context, taste_bitmap4, width, height, 225, 4);
		menu_layout.addView(menuview4);
		taste_bitmap5 = BitmapFactory.decodeResource(getResources(),
				R.drawable.my_center_normal);
		menuview5 = new MenuView(context, taste_bitmap5, width, height, 285, 5);
		menu_layout.addView(menuview5);

	}

	public View addButtonByText(String text) {
		Button btn = new Button(this);
		btn.setText(text);
		return btn;
	}

	public View addTextByText(String text) {
		TextView tv = new TextView(this);
		tv.setText(text);
		tv.setGravity(1);
		return tv;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log.i("Fling", "Activity onTouchEvent!");
		return this.detector.onTouchEvent(event);
	}

	boolean isclick = false;

	public boolean onDown(MotionEvent e) {
		Log.e("TAG", e.getX() + " , " + e.getY());
		if ((e.getX() > width / 2 - taste_bitmap.getWidth())
				&& (e.getX() < width / 2)
				&& (e.getY() > height
						/ 2
						- taste_bitmap.getHeight()
						- BitmapFactory.decodeResource(getResources(),
								R.drawable.choice_point_img).getHeight() / 2)
				&& (e.getY() < height / 2)) {
			isclick = true;
			Log.e("TAG", "按下： = " + indexs);
			switch (indexs) {
			case 0:
				PrefsUtils.setIndex("1");
				IntentUtil.redirectToNext(MainActivity.this,
						com.red.wine.ui.TasteActivity.class);
				break;
			case 1:
				PrefsUtils.setIndex("2");
				IntentUtil.redirectToNext(MainActivity.this,
						com.red.wine.ui.TasteActivity.class);
				break;
			case -5:
				PrefsUtils.setIndex("2");
				IntentUtil.redirectToNext(MainActivity.this,
						com.red.wine.ui.TasteActivity.class);
				break;
			case 2:
				PrefsUtils.setIndex("3");
				IntentUtil.redirectToNext(MainActivity.this,
						com.red.wine.ui.PerodeActivity.class);
				break;
			case -4:
				PrefsUtils.setIndex("3");
				IntentUtil.redirectToNext(MainActivity.this,
						com.red.wine.ui.PerodeActivity.class);
				break;
			case 3:
				IntentUtil.redirectToNext(context,
						com.red.wine.ui.PurchActivity.class);
				break;
			case -3:
				IntentUtil.redirectToNext(context,
						com.red.wine.ui.PurchActivity.class);
				break;
			case 4:
				IntentUtil.redirectToNext(context,
						com.red.wine.ui.CommunityActivity.class);
				break;
			case -2:
				IntentUtil.redirectToNext(context,
						com.red.wine.ui.CommunityActivity.class);
				break;
			case 5:

				IntentUtil.redirectToNext(context,
						com.red.wine.ui.LoginActivity.class);
				break;
			case -1:
				IntentUtil.redirectToNext(context,
						com.red.wine.ui.LoginActivity.class);
				break;
			}
		} else {
			isclick = false;
		}
		return true;
	}

	private void setMenu(int index) {
		// indexs = index;
		if (index == 0) {
			taste_bitmap = BitmapFactory.decodeResource(getResources(),
					R.drawable.taste_press);
		} else {
			taste_bitmap = BitmapFactory.decodeResource(getResources(),
					R.drawable.taste_normal);
		}
		menuview = new MenuView(context, taste_bitmap, width, height, -15, 0);
		// menu_layout.addView(menuview);
		if (index == 1) {
			taste_bitmap1 = BitmapFactory.decodeResource(getResources(),
					R.drawable.appreciate_press);
		} else {
			taste_bitmap1 = BitmapFactory.decodeResource(getResources(),
					R.drawable.appreciate_normal);
		}
		menuview1 = new MenuView(context, taste_bitmap1, width, height, 45, 1);
		// menu_layout.addView(menuview1);
		if (index == 2) {
			taste_bitmap2 = BitmapFactory.decodeResource(getResources(),
					R.drawable.recommend_press);
		} else {
			taste_bitmap2 = BitmapFactory.decodeResource(getResources(),
					R.drawable.recommend_normal);
		}
		menuview2 = new MenuView(context, taste_bitmap2, width, height, 105, 2);
		// menu_layout.addView(menuview2);
		if (index == 3) {
			taste_bitmap3 = BitmapFactory.decodeResource(getResources(),
					R.drawable.purchase_press);
		} else {
			taste_bitmap3 = BitmapFactory.decodeResource(getResources(),
					R.drawable.purchase_normal);
		}
		menuview3 = new MenuView(context, taste_bitmap3, width, height, 165, 3);
		// menu_layout.addView(menuview3);
		if (index == 4) {
			taste_bitmap4 = BitmapFactory.decodeResource(getResources(),
					R.drawable.community_press);
		} else {
			taste_bitmap4 = BitmapFactory.decodeResource(getResources(),
					R.drawable.community_normal);
		}
		menuview4 = new MenuView(context, taste_bitmap4, width, height, 225, 4);
		// menu_layout.addView(menuview4);
		if (index == 5) {
			taste_bitmap5 = BitmapFactory.decodeResource(getResources(),
					R.drawable.my_center_press);
		} else {
			taste_bitmap5 = BitmapFactory.decodeResource(getResources(),
					R.drawable.my_center_normal);
		}
		menuview5 = new MenuView(context, taste_bitmap5, width, height, 285, 5);
		// menu_layout.removeViewInLayout(menuview5);
		// menu_layout.addView(menuview5);
		if (menu_layout != null && menu_layout.getChildCount() > 0) {
			menu_layout.removeAllViews();
		}
		menu_layout.addView(menuview);
		menu_layout.addView(menuview1);
		menu_layout.addView(menuview2);
		menu_layout.addView(menuview3);
		menu_layout.addView(menuview4);
		menu_layout.addView(menuview5);

	}

	/**
	 * 监听滑动
	 */
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > 60) {
			if (rotateAnimation != null) {
				menu_layout.clearAnimation();
				rotateAnimation.cancel();
			}
			showAnimation(menu_layout, true);
			return true;
		} else if (e1.getX() - e2.getX() < -60) {
			if (rotateAnimation != null) {
				menu_layout.clearAnimation();
				rotateAnimation.cancel();
			}
			showAnimation(menu_layout, false);
			return true;
		}
		return true;
	}

	private float last_de;
	private float last_dregree;
	RotateAnimation rotateAnimation;
	private int indexs = 0;

	public void showAnimation(View view, boolean ha) {

		float centerX = view.getWidth() / 2.0f;
		float centerY = view.getHeight() / 2.0f;
		if (!ha) {
			indexs++;
			if (indexs > 5) {
				indexs = 0;
			}
			rotateAnimation = new RotateAnimation(last_dregree,
					last_dregree - 60, centerX, centerY);
			last_dregree = last_dregree - 60;
		} else {
			indexs--;
			if (indexs == -6) {
				indexs = 0;
			}

			rotateAnimation = new RotateAnimation(last_dregree,
					last_dregree + 60, centerX, centerY);
			last_dregree = last_dregree + 60;
		}

		rotateAnimation.setDuration(1000);
		rotateAnimation.setFillAfter(true);
		rotateAnimation.setRepeatMode(1);
		view.startAnimation(rotateAnimation);

		switch (indexs) {
		case 0:
			setMenu(0);
			break;
		case 1:
			setMenu(1);
			break;
		case -5:
			setMenu(1);
			break;
		case 2:
			setMenu(2);
			break;
		case -4:
			setMenu(2);
			break;
		case 3:
			setMenu(3);
			break;
		case -3:
			setMenu(3);
			break;
		case 4:
			setMenu(4);
			break;
		case -2:
			setMenu(4);
			break;
		case 5:
			setMenu(5);
			break;
		case -1:
			setMenu(5);
			break;
		}
	}

	public void onLongPress(MotionEvent e) {

	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {

	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}